package com.yanfan.data.controller.dashBoard;

import com.yanfan.base.service.ISessionStore;
import com.yanfan.common.annotation.Anonymous;
import com.yanfan.common.constant.YanfanConstant;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.core.redis.RedisCache;
import com.yanfan.iot.domain.AlertLog;
import com.yanfan.iot.domain.Device;
import com.yanfan.iot.model.dashBoard.*;
import com.yanfan.iot.service.DashboardService;
import com.yanfan.mqtt.manager.ClientManager;
import com.yanfan.mqtt.manager.RetainMsgManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 首页面板和大屏数据
 *
 * @author gsb
 * @date 2023/3/27 17:00
 */
@Api(tags = "首页面板和大屏数据")
@RestController
@RequestMapping("/dashBoard")
public class DashBoardController extends BaseController {

    @Resource
    private RedisCache redisCache;
    @Resource
    private ISessionStore sessionStore;

    @Resource
    private DashboardService dashboardService;

    @GetMapping("/stats")
    @ApiOperation("mqtt状态数据")
    @Anonymous
    public AjaxResult stats() {
        DashMqttStat stat = DashMqttStat
                .builder()
                .connection_count(sessionStore.getSessionMap().size())
                .connection_total(getTotal(YanfanConstant.REDIS.MESSAGE_CONNECT_TOTAL))
                .subscription_count(ClientManager.topicMap.size())
                .subscription_total(getTotal(YanfanConstant.REDIS.MESSAGE_SUBSCRIBE_TOTAL))
                .retain_count(RetainMsgManager.getSize())
                .retain_total(getTotal(YanfanConstant.REDIS.MESSAGE_RETAIN_TOTAL))
                .session_count(sessionStore.getSessionMap().size())
                .session_total(getTotal(YanfanConstant.REDIS.MESSAGE_CONNECT_TOTAL))
                .build();
        return AjaxResult.success(stat);

    }


    @GetMapping("/metrics")
    @ApiOperation("mqtt统计")
    @Anonymous
    public AjaxResult metrics() {
        DashMqttMetrics metrics = DashMqttMetrics
                .builder()
                .send_total(getTotal(YanfanConstant.REDIS.MESSAGE_SEND_TOTAL))
                .receive_total(getTotal(YanfanConstant.REDIS.MESSAGE_RECEIVE_TOTAL))
                .auth_total(getTotal(YanfanConstant.REDIS.MESSAGE_AUTH_TOTAL))
                .connect_total(getTotal(YanfanConstant.REDIS.MESSAGE_CONNECT_TOTAL))
                .subscribe_total(getTotal(YanfanConstant.REDIS.MESSAGE_SUBSCRIBE_TOTAL))
                .today_received(getTotal(YanfanConstant.REDIS.MESSAGE_RECEIVE_TODAY))
                .today_send(getTotal(YanfanConstant.REDIS.MESSAGE_SEND_TODAY))
                .build();
        return AjaxResult.success(metrics);
    }

    @GetMapping("/deviceCount")
    @ApiOperation("设备数量统计")
    @Anonymous
    public AjaxResult deviceCount() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        DeviceCount deviceCount= dashboardService.deviceCount(device);
        return AjaxResult.success(deviceCount);
    }
    @GetMapping("/alertLog")
    @ApiOperation("智慧照明——异常预警")
    @Anonymous
    public AjaxResult alertLog() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.alertLog(device);
        return AjaxResult.success(list);
    }

    @GetMapping("/onlineRate")
    @ApiOperation("智慧照明——设备在线率")
    @Anonymous
    public AjaxResult onlineRate() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.onlineRate(device));
    }
    @GetMapping("/deviceGroup")
    @ApiOperation("智慧照明——区域设备")
    @Anonymous
    public AjaxResult deviceGroup() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.deviceGroup(device));
    }
    @GetMapping("/deviceIlluminationRate")
    @ApiOperation("智慧照明——设备亮灯率")
    @Anonymous
    public AjaxResult deviceIlluminationRate() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.deviceIlluminationRate(device));
    }
    @GetMapping("/deviceStatus")
    @ApiOperation("智慧配电——设备状态")
    @Anonymous
    public AjaxResult deviceStatus() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.deviceStatus(device));
    }
    @GetMapping("/getAllLight")
    @ApiOperation("查询所有灯具设备")
    @Anonymous
    public AjaxResult getAllLight() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.getAllLight(device));
    }

    @GetMapping("/dlqAlertLog")
    @ApiOperation("智慧配电——告警记录")
    @Anonymous
    public AjaxResult dlqAlertLog() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.dlqAlertLog(device);
        return AjaxResult.success(list);
    }

    @GetMapping("/minorIssues")
    @ApiOperation("智慧配电——轻微问题")
    @Anonymous
    public AjaxResult minorIssues() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.minorIssues(device);
        return AjaxResult.success(list);
    }

    @GetMapping("/monitorDeviceGroup")
    @ApiOperation("智慧安防——设备监控")
    @Anonymous
    public AjaxResult monitorDeviceGroup() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<DonitorDeviceGroup> deviceGroup=dashboardService.monitorDeviceGroup(device);
        return AjaxResult.success(deviceGroup);
    }
    @GetMapping("/monitorOnlineNum")
    @ApiOperation("智慧安防——设备监控")
    @Anonymous
    public AjaxResult monitorOnlineNum() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        MonitorOnlineVo deviceStatus = dashboardService.monitorOnlineNum(device);
        return AjaxResult.success(deviceStatus);
    }

    @GetMapping("/monitorDistributionProportion")
    @ApiOperation("智慧安防——设备分布占比")
    @Anonymous
    public AjaxResult monitorDistributionProportion() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.monitorDistributionProportion(device));
    }
    @GetMapping("/getAreaList")
    @ApiOperation("智慧安防——设备分布占比")
    @Anonymous
    public AjaxResult getAreaList() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.getAreaList(device));
    }

    @GetMapping("/deviceOverview")
    @ApiOperation("总驾驶舱——设备总览")
    @Anonymous
    public AjaxResult deviceOverview() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        Map<String, Object> deviced = dashboardService.deviceOverview(device);
        return AjaxResult.success(deviced);
    }

    @GetMapping("/getCenterData")
    @ApiOperation("总驾驶舱——中心数据")
    @Anonymous
    public AjaxResult getCenterData() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        DeviceOverview deviced = dashboardService.getCenterData(device);
        return AjaxResult.success(deviced);
    }
    @GetMapping("/getWarningData")
    @ApiOperation("总驾驶舱——告警信息")
    @Anonymous
    public AjaxResult getWarningData() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> alertLog = dashboardService.getWarningData(device);
        return AjaxResult.success(alertLog);
    }
    @GetMapping("/getUnderlineData")
    @ApiOperation("总驾驶舱——离线设备列表")
    @Anonymous
    public AjaxResult getUnderlineData() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<DeviceUnderlineData> underlineDatas = dashboardService.getUnderlineData(device);
        return AjaxResult.success(underlineDatas);
    }
    @GetMapping("/getDailyAlertCount")
    @ApiOperation("照明看板——每日告警数")
    @Anonymous
    public AjaxResult getDailyAlertCount() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.getDailyAlertCount(device);
        return AjaxResult.success(list);
    }
    @GetMapping("/getTodayAlert")
    @ApiOperation("照明看板——今日告警列表")
    @Anonymous
    public AjaxResult getTodayAlertCount() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.getTodayAlert(device);
        return AjaxResult.success(list);
    }
    @GetMapping("/getElectricTodayAlert")
    @ApiOperation("配电看板——今日告警列表")
    @Anonymous
    public AjaxResult getElectricTodayAlert() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.getElectricTodayAlert(device);
        return AjaxResult.success(list);
    }
    @GetMapping("/getElectricDailyAlertCount")
    @ApiOperation("配电看板——每日告警数")
    @Anonymous
    public AjaxResult getElectricDailyAlertCount() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        List<AlertLog> list= dashboardService.getElectricDailyAlertCount(device);
        return AjaxResult.success(list);
    }
    @GetMapping("/getElectricDeviceData")
    @ApiOperation("配电看板——设备数量")
    @Anonymous
    public AjaxResult getElectricDeviceData() {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        return AjaxResult.success(dashboardService.getElectricDeviceData(device));
    }
    @GetMapping("/getOnlineData/{categoryName}")
    @ApiOperation("看板——每日在线率")
    @Anonymous
    public AjaxResult getOnlineData(@PathVariable  String categoryName) {
        Device device = new Device();
        device.setDeptId(getLoginUser().getDeptId());
        device.setTenantId(getLoginUser().getUserId());
        device.setCategoryName(categoryName);
        return AjaxResult.success(dashboardService.getOnlineData(device));
    }

    public Integer getTotal(String key) {
        return redisCache.getCacheObject(key) == null ? 0 : redisCache.getCacheObject(key);
    }
}
